package com.Blog.www.servlet;

import com.Blog.www.constant.DataConstant;
import com.Blog.www.constant.PageConstant;
import com.Blog.www.constant.TipsConstant;
import com.Blog.www.po.BlogComment;
import com.Blog.www.po.CommentName;
import com.Blog.www.po.PageBean;
import com.Blog.www.service.BlogService;
import com.Blog.www.service.CommentService;
import com.Blog.www.service.Impl.BlogServiceImpl;
import com.Blog.www.service.Impl.CommentServiceImpl;
import com.Blog.www.service.Impl.UserServiceImpl;
import com.Blog.www.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * @author 君未洋
 */
@WebServlet("/BlogComment/*")
public class CommentServlet extends BaseServlet {

    private static UserService userService = new UserServiceImpl();
    private static BlogService blogService = new BlogServiceImpl();
    private static CommentService blogCommentService = new CommentServiceImpl();

    public void deleteCommentList(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String[] commentIds = req.getParameterValues("commentId");
        if (commentIds == null || commentIds.length == 0) {
            req.getRequestDispatcher("/BlogComment/getCommentList").forward(req, resp);
        }

        req.removeAttribute(PageConstant.COMMENT_A);
        //没问题直接去操作了
        if (blogCommentService.deleteList(commentIds)) {
            req.setAttribute(PageConstant.COMMENT_A, TipsConstant.BLOG_OPERATION_B);
        } else {
            req.setAttribute(PageConstant.COMMENT_A, TipsConstant.BLOG_OPERATION_A);
        }

        req.getRequestDispatcher("/BlogComment/getCommentList").forward(req, resp);
    }

    public void getCommentList(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String currentPage = req.getParameter("currentPage");
        String rows = req.getParameter("rows");
        //熟悉的获取页面数据
        PageBean<CommentName> list = blogCommentService.getCommentList(currentPage, rows);

        req.removeAttribute(DataConstant.COMMENT_E);
        req.setAttribute(DataConstant.COMMENT_E, list);
        req.getRequestDispatcher("/commentList.jsp").forward(req, resp);
    }

    /**
     * 添加博客回复用
     */
    public void addReplyComment(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        //先获取下对应的需要用到的内容
        String bid = req.getParameter("bid");
        String uid = req.getParameter("uid");
        String cid = req.getParameter("cid");
        String comment = req.getParameter("comment");
        String cuid = req.getParameter("cuid");
        req.removeAttribute(PageConstant.BLOG_A);
        if (blogCommentService.addReply(bid, uid, cid, comment)) {
            req.setAttribute(PageConstant.BLOG_A, TipsConstant.BLOG_REPLY_A);
        } else {
            req.setAttribute(PageConstant.BLOG_A, TipsConstant.BLOG_REPLY_B);
        }
        req.getRequestDispatcher("/BlogComment/replyComment?bid=" + bid + "&prior=" + cid + "&cuid=" + cuid).forward(req, resp);
    }

    /**
     * 查看博客回复用
     */
    public void replyComment(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        //先获取下对应的需要用到的内容
        String getBid = req.getParameter("bid");
        String getPrior = req.getParameter("prior");
        String getCuid = req.getParameter("cuid");
        //校验一下,然后传入
        if (getBid == null || "".equals(getBid) || getPrior == null || "".equals(getPrior)) {
            //有问题就直接返回原来的博客页面
            req.getRequestDispatcher("/UserBlog/Blog?id=" + getBid);
        } else {
            int bid = Integer.parseInt(getBid);
            int prior = Integer.parseInt(getPrior);

            //通过用户id获取下用户名
            String username = userService.getUsername(getCuid);

            req.removeAttribute(DataConstant.COMMENT_A);
            req.setAttribute(DataConstant.COMMENT_A, username);
            //传入数据获取回复集合
            List<BlogComment> commentList = blogCommentService.getCommentList(bid, prior);
            BlogComment comment = blogCommentService.getComment(prior);
            req.removeAttribute(DataConstant.COMMENT_B);
            req.setAttribute(DataConstant.COMMENT_B, comment);
            req.removeAttribute(DataConstant.COMMENT_C);
            req.setAttribute(DataConstant.COMMENT_C, commentList);
            req.getRequestDispatcher("/Reply.jsp").forward(req, resp);
        }
    }

    /**
     * 删除博客对应评论
     */
    public void deleteComment(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

        //先获取下对应博客的id
        String cid = req.getParameter("cid");
        String bid = req.getParameter("bid");
        //通过service后返回
        req.removeAttribute(PageConstant.BLOG_B);
        if (blogCommentService.deleteComment(cid)) {
            req.setAttribute(PageConstant.BLOG_B, TipsConstant.BLOG_COMMENT_A);
        } else {
            req.setAttribute(PageConstant.BLOG_B, TipsConstant.BLOG_COMMENT_B);
        }
        req.getRequestDispatcher("/UserBlog/Blog?id=" + bid).forward(req, resp);

    }

    /**
     * 博客评论添加方法
     */
    public void addComment(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        //先获取下对应的需要用到的内容
        String bid = req.getParameter("bid");
        String uid = req.getParameter("uid");
        String comment = req.getParameter("comment");

        req.removeAttribute(PageConstant.BLOG_B);
        if (blogCommentService.addComment(bid, uid, comment)) {
            req.setAttribute(PageConstant.BLOG_B, TipsConstant.BLOG_COMMENT_C);
        } else {
            req.setAttribute(PageConstant.BLOG_B, TipsConstant.BLOG_COMMENT_D);
        }
        req.getRequestDispatcher("/UserBlog/Blog?id=" + bid).forward(req, resp);
    }

}
